<?php
namespace Admin\Controller;


class ProductController extends AdminController {

    public function index(){





        $classifyModel = M('Classify');
        $map = array();
        $classify_list = $this->lists($classifyModel,$map,'id DESC');
        $new_classify_list = array();
        foreach($classify_list as $key => &$value){
            $new_classify_list[$value['id']] = $value['title'];
        }

//        echo "<pre>".print_r($new_classify_list,true)."</pre>";
        $newsModel = M('Product');

        $map = array();
        $list = $this->lists($newsModel,$map,'id DESC');

        foreach($list as $key => &$value){

            $value['classify_name'] = $new_classify_list[$value['classify_id']];
        }
        $this->assign('_list', $list);
        $this->display();
    }


    public function add(){
        if(IS_POST){
            $title = I('title');
            $path = I("path");
            $classify_id = I("classify_id");
            $iphone = I('iphone');
            $abstract = I('abstract');

            if(!$title || !$path || !$classify_id || !$iphone || !$abstract){
                $this->error('输入不能为空！');
            }

            $id = I("id",0);

            $model = M("Product");

            $data['title'] = $title;
            $data['path'] = $path;
            $data['classify_id'] = $classify_id;
            $data['iphone'] = $iphone;
            $data['abstract'] = $abstract;
            $data['create_time'] = time();

            if($id){
                $re = $model->where("id=".$id)->save($data);
                $msg = "编辑";
            }else{
                $re = $model->add($data);
                $msg = "成功";
            }

            if($re){

                $this->success($msg."成功",U("Product/index"));
            }else{
                $this->error($msg."失败");
            }
        }else{

            $id = I("id");
            if($id){
                $model = M("Product");
                $data = $model->where(array('id'=>$id))->find();
//                echo "<pre>".print_r($data,true)."</pre>";
                $this->assign("data",$data);
            }

            $classifyModel = M('Classify');

            $map = array();
            $list = $this->lists($classifyModel,$map,'id DESC');
//            var_dump($list);
            $this->assign('_list', $list);

            $this->display();
        }
    }

    public function upload(){

//        echo "<pre>".print_r($_FILES,true)."</pre>";
        if($_FILES['file']['error'] == 0){
            $filename = 'upload/product/'.time(). substr($_FILES['file']['name'], strrpos($_FILES['file']['name'], '.'));
            move_uploaded_file($_FILES['file']['tmp_name'],$filename);
            echo json_encode(array('url'=>$filename));
        }
    }

    public function del(){
        $id = (int)I('id');

        if(!$id){
            echo json_encode(array('errno'=>0));
            die;
        }
        $model = M('Product');
        $re = $model->where("id=".$id)->delete();
        if($re){
            echo json_encode(array('errno'=>1));
            die;
        }else{
            echo json_encode(array('errno'=>0));
            die;
        }
    }
}